﻿using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Text;

namespace YhdSdk
{
    [Serializable]
    public abstract class YhdResponse
    {
        /// <summary>
        /// 错误码
        /// </summary>
        [XmlArray("errInfoList")]
        [XmlArrayItem("errDetailInfo")]
        public List<ErrDetailInfo> ErrInfoList
        {
            get;
            set;
        }

        /// <summary>
        /// 失败记录数
        /// </summary>
        [XmlElement("errorCount")]
        public Int32 ErrorCount
        {
            get;
            set;
        }

        /// <summary>
        /// 错误消息
        /// </summary>
        public String ErrorMsg
        {
            get
            {
                return this.FormatErrorInfo(this.ErrInfoList);
            }
        }

        /// <summary>
        /// 响应原始内容
        /// </summary>
        public string Body
        {
            get;
            set;
        }

        /// <summary>
        /// HTTP GET请求的URL
        /// </summary>
        public string ReqUrl
        {
            get;
            set;
        }

        /// <summary>
        /// 响应结果是否错误
        /// </summary>
        public bool IsError
        {
            get
            {
                return ErrInfoList != null && ErrInfoList.Count > 0;
            }
        }

        /// <summary>
        /// 格式化错误消息
        /// </summary>
        /// <param name="errInfos"></param>
        /// <returns></returns>
        private String FormatErrorInfo(IEnumerable<ErrDetailInfo> errInfos)
        {
            if (errInfos == null)
            {
                return String.Empty;
            }

            StringBuilder sbMsg = new StringBuilder(10240);
            foreach (var item in errInfos)
            {
                sbMsg.AppendFormat("错误代码：{0}，错误描述：{1}，主键信息：{2}。", item.ErrorCode, item.ErrorDes, item.PkInfo);
            }

            return sbMsg.ToString();
        }

    }
}
